package cn.itsource.hrm.controller;

import cn.itsource.hrm.domain.Sso;
import cn.itsource.hrm.jwt.JwtUtils;
import cn.itsource.hrm.jwt.Payload;
import cn.itsource.hrm.service.ITenantTypeService;
import cn.itsource.hrm.domain.TenantType;
import cn.itsource.hrm.query.TenantTypeQuery;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import cn.itsource.hrm.util.RsaUtils;
import cn.itsource.hrm.util.UserContext;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
import java.util.Enumeration;
import java.util.List;

/**
 * restfull
 *   用路径表示资源  /tenantType/1
 *   用http请求方式表示操作 get表示获取 delete来表示删除 put add post update
 */
@RestController
@RequestMapping("/tenantType")
public class TenantTypeController {
    @Autowired
    public ITenantTypeService tenantTypeService;


    /**
     * 保存和修改公用的
     * @param tenantType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody TenantType tenantType){
        try {
            if( tenantType.getId()!=null)
                tenantTypeService.updateById(tenantType);
            else
                tenantTypeService.insert(tenantType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            tenantTypeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public TenantType get(@PathVariable("id")Long id)
    {
        return tenantTypeService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<TenantType> list(){

        return tenantTypeService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象，分页条件+查询条件
    * @return PageList 分页对象
    */
    /*
    @PostMapping("/list")
    public PageList<TenantType> json(@RequestBody TenantTypeQuery query)
    {


        //方案1：EntityWrapper
        //1 高级查询
        EntityWrapper<TenantType> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword())
                .or()
                .like("description",query.getKeyword());

        //2 分页
        Page<TenantType> page = new Page<TenantType>(query.getPage(),query.getRows()); //当前页，每页显示多少条
        page = tenantTypeService.selectPage(page,wrapper);
        return new PageList<TenantType>(page.getTotal(),page.getRecords());
    }*/
    //方案2：就和传统一样自己写sql，ResultMap
    @PostMapping("/list")
    public PageList<TenantType> json(@RequestBody TenantTypeQuery query, HttpServletRequest request)
    {
        /*
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            System.out.println(headerNames.nextElement());
        }
        System.out.println("================");
        String jwtToken = request.getHeader("jwtToken");
        PublicKey publicKey = RsaUtils.getPublicKey(TenantTypeController.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
        Payload<Sso> payload = JwtUtils.getInfoFromToken(jwtToken, publicKey, Sso.class);
        Sso sso = payload.getUserInfo();
        System.out.println(sso);
        System.out.println(jwtToken);
         */
        Sso sso = UserContext.currentSso(request);
        System.out.println(sso);
        return tenantTypeService.selectPageList(query);
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try
        {
            tenantTypeService.deleteBatchIds(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！");
        }


    }
}
